import { Component } from '@angular/core';
import { NavController, NavParams,ViewController,PopoverController,ModalController,AlertController } from 'ionic-angular';
import { Mistake } from '../mistake';
import { SemesterChoosePage } from '../semester-choose/semester-choose';

@Component({
    selector: 'page-mistake-choose-points',
    templateUrl: 'choose-points.html',
   
  })
  export class ChoosePointsPage{
      misQuestion: Mistake;
      substitutePointList:Array<{name:string,flag:boolean}>;
      latestPointList:string[];
      pointsSelected:string[];
      pointsArray:Array<{name:string,flag:boolean}>;
      addNewPoint:string = "";
      points:Array<{name:string,flag:boolean}>;
      radiogroup:string;
      semesterList;
      testCheckboxOpen: boolean;  
      testCheckboxResult;
      temp:string="233";
      show:string="234";

      constructor(public navparams:NavParams,
                  public navCtrl:NavController,
                  public viewCtrl:ViewController,
                  public modalCtrl:ModalController,
                  public alertCtrl:AlertController,
                  public popoverCtrl: PopoverController){
        this.pointsArray = [];
        this.radiogroup = "";
        this.pointsSelected = [];
        this.points = [];
        this.pointsArray = [];
        this.radiogroup = "";
        this.pointsSelected = [];
        this.substitutePointList = [];
        this.latestPointList = [];
        this.points = [];
        this.temp="1";
        this.show="2";
        this.testCheckboxOpen = false;
        this.testCheckboxResult = "七年级上学期";


        this.points.push({name: "其他知识点1", flag:false});
        this.points.push({name: "诗词鉴赏", flag:false});
        this.points.push({name: "以偏概全", flag:false});
        this.points.push({name: "其他知识点2", flag:false});
     

        //this.substitutePointList = this.navparams.get("substitutePointList");
        this.substitutePointList.push({name: "point1", flag:false});
        this.substitutePointList.push({name: "point2", flag:false});
        this.substitutePointList.push({name: "point3", flag:false});
        this.substitutePointList.push({name: "point4", flag:false});

        this.latestPointList.push("latest1");
        this.latestPointList.push("latest2");
        this.latestPointList.push("latest3");


        
       // this.pointsSelected = this.navparams.get("pointsSelected");
        this.pointsSelected = [];
        this.pointsArray.push({name: "知识点1", flag:false});
        this.pointsArray.push({name: "牛顿第二定律", flag:false});
        this.pointsArray.push({name: "知识点3", flag:false});
        this.pointsArray.push({name: "牛顿第三定律", flag:false});
        this.pointsArray.push({name: "开普勒第二定律", flag:false});
        this.pointsArray.push({name: "开普勒第三定律", flag:false});
        
        this.semesterList = [];
        this.semesterList.push("七年级上学期");
        this.semesterList.push("七年级下学期");
        this.semesterList.push("八年级上学期");
        this.semesterList.push("八年级下学期");
        this.semesterList.push("九年级上学期");
        this.semesterList.push("九年级下学期");
      }

      getPoints(num:number){
        if(this.pointsArray.length>0){
        this.pointsArray[num].flag = true;
        this.pointsSelected.push(this.pointsArray[num].name);
        }
      }

      removeSubstitutePoints(num:number){
        this.substitutePointList[num].flag = false;
      }

      addSubstitutePoints(num:number){
        this.substitutePointList[num].flag = true; 
      }

      removeLatestPoints(num:number){
        this.pointsArray[num].flag = false;
      }

      addLatestPoints(num:number){
        this.pointsArray[num].flag = true;      
      }


      removePoints(num:number){
        if(this.pointsArray.length>0){
        this.pointsArray[num].flag = false;
        this.pointsSelected.splice(num);
        }
      }

      addPoint(){
        if(this.addNewPoint != ""){
        this.pointsArray.push({name:this.addNewPoint, flag:false});
        //this.pointsSelected.push(this.addNewPoint);
        }
      }

      chooseSemester(){
        //this.navCtrl.push(SemesterChoosePage,{});
        let alert = this.alertCtrl.create();
        alert.setTitle('选择学期');
        this.semesterList.forEach(element => {
          alert.addInput({
            type: 'radio',
            label: element,
            value: element,
            checked: false
          });
         });
        

         alert.addButton({
           text:'Cancel',
          handler:data => {
            this.temp = this.show;
          }});
         alert.addButton({
           text: 'OK',
           handler: data => {
             this.testCheckboxOpen = false;
             this.testCheckboxResult = data;
             console.log(this.testCheckboxResult);
             this.temp = this.show;
           }
         });
         alert.present();
        
       
       }


       addAll(){
        //最近的知识点
         for(var i = 0; i < this.substitutePointList.length; i++){
             if(this.substitutePointList[i].flag == true){
               this.pointsSelected.push(this.substitutePointList[i].name);
             }
           }
        //匹配的知识点
         for(var j = 0; j < this.pointsArray.length; j++){
             if(this.pointsArray[j].flag == true){
               this.pointsSelected.push(this.pointsArray[j].name);
             }
           }
   
           return this.pointsSelected;
       }
   
         choose(item){
           if(item != "undefined" && item !=null){
             this.pointsSelected.push(item.name);
           }
         }
   

        confirm(){
        if(this.pointsSelected = this.addAll()){
          this.viewCtrl.dismiss(this.pointsSelected);
        }
      

      }
      
      openShow(){
        this.temp="1";
        this.show="1";
      }

      closeShow(){
        this.show="2";
      }

      close(item){
      if(item != "undefined" && item !=null){
        console.log(item);
        this.pointsSelected.push(item);
      }
    
      this.viewCtrl.dismiss(this.pointsSelected);

      }

      return(){
        
            this.viewCtrl.dismiss([]);
        
      }

  }